using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance { get; private set; }
    public Bird[] birdList;
    private int index = -1;
    private int pigTotalCount;//猪的数量
    private int currentPigTotalCount;//剩余猪的数量
    public Transform parentBird;

    [Header("场景")]
    [SerializeField] private SceneField MenuScenes;//菜单场景
    [SerializeField] private SceneField LevelScenes;//关卡场景
    [SerializeField] private SceneField GameScenes;//游戏场景

    [Header("UI")]
    [SerializeField] private GameObject PauseUI;//暂停UI
    [SerializeField] private GameObject GameOverUI;//游戏结束UI

    private GameObject[] bird;
    private GameObject[] levels;
    private int levelsIndex = -1;

    private void Awake()
    {
        Instance = this;
    }

    private void Start() {
        Init();
    }

    void Init()
    {
        Time.timeScale = 1;

        if (bird == null) bird = Resources.LoadAll<GameObject>("Prefabs/Bird");
        if (levels == null) levels = Resources.LoadAll<GameObject>("Level");
        //随机生成鸟
        int birdsIndex = 0;
        for (var i = 0; i < 4; i++)
        {
            birdsIndex = Random.Range(0, 4);
            GameObject go = Instantiate(bird[birdsIndex], transform.position, Quaternion.identity);
            go.transform.SetParent(parentBird, false);
            go.transform.localPosition = new Vector2(-i * 1, 0);
        }

        //随机生成关卡场景
        levelsIndex = Random.Range(0, levels.Length);
        Instantiate(levels[levelsIndex]);

        //在当前场景中查找所有带Bird脚本的对象，不进行排序
        birdList = FindObjectsByType<Bird>(FindObjectsSortMode.None);
        currentPigTotalCount = pigTotalCount = FindObjectsByType<Pig>(FindObjectsSortMode.None).Length;

        LoadNextBird();
    }


    public void LoadNextBird()
    {
        index++;
        if (index >= birdList.Length)
        {
            GameEnd();
        }
        else
        {
            birdList[index].SetStart(Slingshot.Instance.shootPoint.transform.position);
            Camera.main.GetComponent<FollowTarget>().SetTarget(birdList[index].transform);//设置摄像机跟随目标
        }

    }

    public void OnPigDead()
    {
        currentPigTotalCount--;
        if (currentPigTotalCount <= 0)
        {
            GameEnd();
        }
    }

    void GameEnd()
    {
        GameOverUI.SetActive(true);
        if (pigTotalCount == currentPigTotalCount)
        {
            //失败
            GameOverUI.transform.Find("失败").gameObject.SetActive(true);
            AudioManager.Instance.PlaySFX("失败");
        }
        else
        {
            AudioManager.Instance.PlaySFX("胜利");
            int length = 0;//星数
            if (currentPigTotalCount == 0)
            {
                length = 3;
            }
            else if (currentPigTotalCount == 1)
            {
                length = 2;
            }
            else
            {
                length = 1;
            }
            if (length > List.Instance.levelList.list[List.Instance.index].num)
            {
                //添加已经获得的星星数
                List.Instance.levelList.currentNum += length - List.Instance.levelList.list[List.Instance.index].num;
                List.Instance.levelList.list[List.Instance.index].num = length;//修改当前星数
            }
            //解锁下一关
            if (List.Instance.levelList.list[List.Instance.index + 1].num == -1)
            {
                List.Instance.levelList.list[List.Instance.index + 1].num = 0;
            }

            List<LevelList> levelList = DataManager.Instance.Load();
            levelList[List.Instance.levelListIndex] = List.Instance.levelList;
            DataManager.Instance.Save(levelList);

            StartCoroutine(DelayedAction(length));
        }
    }

    IEnumerator DelayedAction(int length)
    {
        for (var i = 0; i < length; i++)
        {
            AudioManager.Instance.PlaySFX("加星");
            GameOverUI.transform.Find("星" + (i + 1)).gameObject.SetActive(true);
            yield return new WaitForSeconds(1.0f); // 等待1秒
        }
    }

    //加载菜单场景
    public void LoadMenuScenes()
    {
        AudioManager.Instance.PlaySFX("点击");
        SceneManager.LoadScene(MenuScenes);
    }

    //加载关卡场景
    public void LoadLevelScenes()
    {
        AudioManager.Instance.PlaySFX("点击");
        SceneManager.LoadScene(LevelScenes);
    }

    //加载游戏场景
    public void LoadGameScenes()
    {
        AudioManager.Instance.PlaySFX("点击");
        SceneManager.LoadScene(GameScenes);
    }


    //重新加载当前场景
    public void ReloadScenes()
    {
        AudioManager.Instance.PlaySFX("点击");
        StopAllCoroutines();
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    //暂停游戏
    public void PauseGame()
    {
        AudioManager.Instance.PlaySFX("点击");
        PauseUI.SetActive(true);
        Time.timeScale = 0;
    }

    //继续游戏
    public void ReloadGame()
    {
        AudioManager.Instance.PlaySFX("点击");
        Time.timeScale = 1;
        PauseUI.SetActive(false);
    }
}
